
package com.lt.test.provider.dao.impl;

import com.lt.test.common.util.StringUtils;
import com.lt.test.provider.dao.BaseDaoSupport;
import com.lt.test.provider.dao.KtShopInstockOrderDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Repository
public class KtShopInstockOrderDaoImpl extends BaseDaoSupport implements KtShopInstockOrderDao {
	
	private static final Logger log = Logger.getLogger(KtShopInstockOrderDaoImpl.class); 

	/**
	 * 根据查询条件获取实现集
	 */
	public List query(Map parameter) {
		List list = new ArrayList();
		try {  
			list = this.queryForList("queryKtShopInstockOrders",parameter);  
		} catch (Exception e) {
			log.error("错误:");
			e.printStackTrace();
		}
	    return list;
	}
	

	/**
	 * 创建实体
	 */
    public int create(Map parameter)
    {  
    	return this.insert("createKtShopInstockOrder", parameter);
    }
    
    /**
     * 更新实体
     */
    public int update(Map parameter)
    {
    	return this.update("updateKtShopInstockOrder",parameter);
    }
     
    /**
     * 保存实体
     */
    public int save(Map parameter)
    {
    	return this.update("saveKtShopInstockOrder",parameter);
    } 
    
  	
  	/**
     * 批量插入
     */
	public int batch(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.insert("batchKtShopInstockOrder",values); 
  		return iEffectedRow;
	}
  	
  	/**
     * 批量更新
     */
  	public int batchUpdate(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.update("batchUpdateKtShopInstockOrder",values);
  		return iEffectedRow;
	}
  	
  	/**
     * 批量删除
     */
  	public int batchDelete(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.delete("batchDeleteKtShopInstockOrder",values);
  		return iEffectedRow;
	}


	@Override
	public Map<String, Object> getInstockOrderListSize(String id, String createUser, String shopId, String remark,
                                                       String approveFlag, String beginDate, String endDate, String type) {
		// TODO Auto-generated method stub
		StringBuilder sqlStr = new StringBuilder();
		LinkedList<Object> params = new LinkedList<Object>();
		sqlStr.append("SELECT count(1) total from KT_SHOP_APP_INSTOCK_ORDER_VIEW t1 WHERE 1=1 ");
		if(!StringUtils.isNullOrEmpty(beginDate)){
			sqlStr.append(" AND t1.CREATE_TIME >= TO_DATE(?, 'yyyy-mm-dd') ");
			params.add(beginDate);
		}
		
		if(!StringUtils.isNullOrEmpty(endDate)){
			sqlStr.append(" AND t1.CREATE_TIME <= TO_DATE(?, 'yyyy-mm-dd') ");
			params.add(endDate);
		}
		if(!StringUtils.isNullOrEmpty(id)){
			sqlStr.append(" AND t1.ID like ? ");
			params.add("%" + id + "%");
		}
		if(!StringUtils.isNullOrEmpty(remark)){
			sqlStr.append(" AND t1.REMARK like ? ");
			params.add("%" + remark + "%");
		}
		if(!StringUtils.isNullOrEmpty(createUser)){
			sqlStr.append(" AND t1.CREATE_USER = ? ");
			params.add(createUser);
		}
		if(!StringUtils.isNullOrEmpty(shopId)){
			sqlStr.append(" AND t1.SHOP_ID in ("+ shopId +") ");
//			params.add(shopId);
		}
		if(!StringUtils.isNullOrEmpty(type)){
			sqlStr.append(" AND t1.TYPE = ? ");
			params.add(type);
		}
		if(!StringUtils.isNullOrEmpty(approveFlag)){
			 if(!"-1".equals(approveFlag)){
				 if("1".equals(approveFlag)||"3".equals(approveFlag)){
					 sqlStr.append(" AND t1.APPROVE_FLAG = ? ");
					 params.add(approveFlag);
				 }else{
					 sqlStr.append(" AND t1.APPROVE_FLAG IS NULL ");
				 }
			 }
		}
		return this.query(sqlStr.toString(), params.toArray());
	}


	@Override
	public List<Map<String, Object>> getInstockOrderListByPage(String id, String createUser, String shopId,
                                                               String remark, String approveFlag, String beginDate, String endDate, Long beginNum, Long endNum, String type) {
		// TODO Auto-generated method stub
		StringBuilder sqlStr = new StringBuilder();
		LinkedList<Object> params = new LinkedList<Object>();
		sqlStr.append("SELECT * FROM (SELECT t.*,ROWNUM rn FROM (SELECT * from KT_SHOP_APP_INSTOCK_ORDER_VIEW t1  WHERE 1=1 ");
		if(!StringUtils.isNullOrEmpty(beginDate)){
			sqlStr.append(" AND t1.CREATE_TIME >= TO_DATE(?, 'yyyy-mm-dd') ");
			params.add(beginDate);
		}
		
		if(!StringUtils.isNullOrEmpty(endDate)){
			sqlStr.append(" AND t1.CREATE_TIME <= TO_DATE(?, 'yyyy-mm-dd') ");
			params.add(endDate);
		}
		if(!StringUtils.isNullOrEmpty(id)){
			sqlStr.append(" AND t1.ID like ? ");
			params.add("%" + id + "%");
		}
		if(!StringUtils.isNullOrEmpty(remark)){
			sqlStr.append(" AND t1.REMARK like ? ");
			params.add("%" + remark + "%");
		}
		if(!StringUtils.isNullOrEmpty(createUser)){
			sqlStr.append(" AND t1.CREATE_USER = ? ");
			params.add(createUser);
		}
		if(!StringUtils.isNullOrEmpty(shopId)){
			sqlStr.append(" AND t1.SHOP_ID in ("+ shopId +") ");
//			params.add(shopId);
		}
		if(!StringUtils.isNullOrEmpty(type)){
			sqlStr.append(" AND t1.TYPE = ? ");
			params.add(type);
		}
		if(!StringUtils.isNullOrEmpty(approveFlag)){
			 if(!"-1".equals(approveFlag)){
				 if("1".equals(approveFlag)||"3".equals(approveFlag)){
					 sqlStr.append(" AND t1.APPROVE_FLAG = ? ");
					 params.add(approveFlag);
				 }else{
					 sqlStr.append(" AND t1.APPROVE_FLAG IS NULL ");
				 }
			 }
		}

		sqlStr.append(" ORDER BY t1.CREATE_TIME DESC)t WHERE  ? >=ROWNUM )t WHERE   t.rn >=? ");
		params.add(endNum);
		params.add(beginNum);
		
		return this.queryAsArray(sqlStr.toString(), params.toArray());
	}

	@Override
	public int updateApp(Map parameter){
		String id = parameter.get("id").toString();
  		String remark = parameter.get("remark").toString();
		StringBuilder sqlStr = new StringBuilder();
		LinkedList<Object> params = new LinkedList<Object>();
		sqlStr.append(" UPDATE D_SHOP_INSTOCK_INDEX SET REMARK = ? where ID = ? ");
		params.add(remark);
		params.add(id);
		return this.update(sqlStr.toString(), params.toArray());
	}

	@Override
	public Map<String, Object> getRecord(String id) {
		// TODO Auto-generated method stub
		StringBuilder sqlStr = new StringBuilder();
		sqlStr.append("SELECT 1 FROM KT_SHOP_INSTOCK_ORDER WHERE ID = ? AND STATUS = '1'");
		return this.query(sqlStr.toString(), new Object[]{id});
	}
}
